package com.bocomm.tracevisualization.api.result;



import com.bocomm.tracevisualization.api.vo.BaseResult;
import lombok.Data;

import java.io.Serializable;

/**
 * comment: http统一返回结果
 *
 * @author: lipengfei
 * @date: 11/05/2019
 */
@Data
public class Result<T> implements Serializable {
	private static final long serialVersionUID = -5736951573549764512L;
	/**
	 * 状态
	 */
	private int status;
	/**
	 * msg
	 */
	private String message;
	/**
	 * 返回数据
	 */
	private T data;

	private Result(){
		this.status = CodeMessage.SUCCESS.getCode();
		this.message = CodeMessage.SUCCESS.getMessage();
	}

	private Result(T data) {
		this.status = CodeMessage.SUCCESS.getCode();
		this.message = CodeMessage.SUCCESS.getMessage();
		this.data = data;
	}
	private Result(CodeMessage codeMessage,T data) {
		this.status = codeMessage.getCode();
		this.message = codeMessage.getMessage();
		this.data = data;
	}

	private Result(CodeMessage codeMessage) {
		if (null == codeMessage) {
			return;
		}
		this.status = codeMessage.getCode();
		this.message = codeMessage.getMessage();
	}

	/**
	 * 成功
	 *
	 * @param <T>
	 * @return
	 */
	public static <T> Result<T> success(T data) {
		return new Result<T>(data);
	}	/**
	 * 成功
	 *
	 * @param <T>
	 * @return
	 */
	public static <T> Result<T> success() {
		return new Result<T>();
	}
	/**
	 * 手动提交响应结果
	 *
	 * @param <T>
	 * @return
	 */
	public static <T extends BaseResult> Result<T> returnResult(T data) {
		CodeMessage resultCode = data.getResultCode();
		return new Result<T>(resultCode,data);
	}

	/**
	 * 失败
	 *
	 * @param <T>
	 * @return
	 */
	public static <T> Result<T> fail(CodeMessage codeMessage) {
		return new Result<T>(codeMessage);
	}

	
}
